package com.atgugui.eduucenter.service.impl;

import com.atgugui.commonutils.JwtUtils;
import com.atgugui.commonutils.MD5Utils;
import com.atgugui.commonutils.Result;
import com.atgugui.commonutils.ResultCode;
import com.atgugui.eduucenter.entity.UcenterMember;
import com.atgugui.eduucenter.entity.vo.RegisterVo;
import com.atgugui.eduucenter.mapper.UcenterMemberMapper;
import com.atgugui.eduucenter.service.UcenterMemberService;
import com.atgugui.servicebase.exceptionhandler.GuLiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-09-11
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 手机号+密码登录
     * @param ucenterMember
     * @return
     */
    @Override
    public String login(UcenterMember ucenterMember) {
        // 获取用户登录时输入的手机号和密码
        String phone = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        // 判断输入是否为空
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)) {
            throw new GuLiException(ResultCode.ERROR,"用户登录信息输入为空");
        }

        // 验证该用户是否注册
        QueryWrapper<UcenterMember> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",phone); // 按照手机号查询用户是否已经注册
        UcenterMember selectMember = baseMapper.selectOne(memberQueryWrapper);
        if (StringUtils.isEmpty(selectMember)) {
            throw new GuLiException(ResultCode.ERROR,"手机号为"+phone+"的用户为注册，请先注册在登陆");
        }

        // 该用户已经注册，验证登陆密码是否正确
        // 注意：数据库中存放的密码是通过加密算法加密之后的值，所以我们需要将前端传递过来的
        // 明文加密之后进行比较
        if (!MD5Utils.encrypt(password).equals(selectMember.getPassword())) {
            throw new GuLiException(ResultCode.ERROR,"登陆密码有误");
        }

        // 登陆信息正确,判断该用户是由被禁止登陆
        if (selectMember.getIsDisabled()) {
            throw new GuLiException(ResultCode.ERROR,"手机号为" + phone + "的用户被禁止登陆，请解除之后登陆");
        }

        // 登陆信息正确，而且为被禁止登陆
        // 使用JWT 生成该用户的唯一令牌 token
        String token = JwtUtils.getJwtToken(selectMember.getId(), selectMember.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 获取注册表单中的用户输入
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String email = registerVo.getEmail();
        String activeCode = registerVo.getCode();

        // 判断用户输入是否为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password) || StringUtils.isEmpty(email) || StringUtils.isEmpty(activeCode)) {
            throw new GuLiException(ResultCode.ERROR,"注册表单输入不能为空");
        }

        // 判断激活码是否和正确
        String activedCode = (String) redisTemplate.opsForValue().get(email);
        if (!activeCode.equals(activedCode)) {
            throw new GuLiException(ResultCode.ERROR,"激活码输入有误");
        }

        // 判断注册的手机号号是否重复
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        int count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new GuLiException(ResultCode.ERROR,"当前手机号已经被其他人注册，请更换手机号重新注册");
        }

        // 注册成功  数据添加数据库中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5Utils.encrypt(password));//密码需要加密的
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        UcenterMember ucenterMember = baseMapper.selectById(openid);
        return ucenterMember;
    }

    @Override
    public int countRegister(String day) {
        int count = baseMapper.countRegister(day);
        return count;
    }
}
